package com.wisdytech.common.handler;

import com.alibaba.fastjson.JSON;
import com.wisdytech.common.bean.StandardConfigResult;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.exception.ConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>ClassName: ExceptionHandler</p>
 * <p>Description: 异常处理类</p>
 * <p>Author: PC</p>
 * <p>Date: 2017-10-17</p>
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    @ExceptionHandler
    @ResponseBody
    public Object resolveException(HttpServletRequest request, HttpServletResponse response,
                                   Exception ex) {
        logger.error("系统异常", ex);

        String errorMessge = "系统异常";
        String errorResponse;

        if (ex instanceof ConfigurationException) {//组态设计器返回值不同
            errorResponse = JSON.toJSONString(StandardConfigResult.getErrorResult(
                    ((ConfigurationException) ex).getCode(), ex.getMessage()));
        } else {
            errorResponse = JSON.toJSONString(StandardResult.getFailResult(errorMessge));
        }

        return errorResponse;
    }





}
